package com.cheng.io.demo;

import java.io.*;

/**
 * 简单文件复制，使用缓冲字符输入(BufferedReader)输出(BufferedWriter)流实现
 */
public class FileCopyByBufferedReaderWriterImpl {
    public static void main(String[] args) {
        String oldPath = "io/resources/特性.txt";
        String newPath = "io/resources/特性1.txt";

        try (
                BufferedReader bufferedReader = new BufferedReader(new FileReader(oldPath));
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newPath));
        ) {
            String readLine;
            boolean isFirstLine = true; // 标记是否是第一行
            while ((readLine = bufferedReader.readLine()) != null){
                //如果是第一行将标记为false
                if (!isFirstLine) {
                    bufferedWriter.newLine(); // 如果不是第一行，先写换行符
                } else {
                    isFirstLine = false; // 第一行之后标记为false
                }
                bufferedWriter.write(readLine);
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
